//
//  PackageViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/10/8.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class PackageViewController: KMViewController {
    
    let packageView = PackageView()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = myPackage
        addRightButton()
        addSubView()
        getData()
        
    }
    
    //MARK: 添加右边按钮
    func addRightButton() {
        
        let barButton = UIBarButtonItem.init(image: #imageLiteral(resourceName: "cardadd"), style: .plain, target: self, action: #selector(rightAction))
        self.navigationItem.rightBarButtonItem = barButton

    }
    
    //MARK: 右边点击事件
    @objc fileprivate func rightAction() {
        
        let addCardVC = AddCardViewController()
        addCardVC.addBlock = {
            
            self.getData()
        }
        self.navigationController?.pushViewController(addCardVC, animated: true)
        
    }
    
    //MARK: 添加子视图
    fileprivate func addSubView() {
        
        let headView = PackageHeadView()
        headView.delegate = self
        self.view.addSubview(headView)
        
        headView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 60))
        }
        
        packageView.creditView.deleteDelegate = self
        packageView.debitView.deleteDelegate = self
        self.view.addSubview(packageView)
        
        packageView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(headView.snp.bottom)
        }
    }
    
    //MARK: 获取数据
    fileprivate func getData() {
        
        KMTool.waiting()
        
        KMTool.requestMethod(.post, URLString: url_bankCardList, parameters: ["user_name":KMTool.stringForEnumKey(.phone) ?? ""], success: { (value, json) in
            
            KMTool.stop()
            
            if json["status"].stringValue == "0" {
                
                self.packageView.creditView.dataArray.removeAll()
                self.packageView.debitView.dataArray.removeAll()
                
                if let data = json["data"].arrayObject {
                    
                    for item in data {
                        let model = PackbackModel.setModel(with: item as! [String : Any])
                        if model.card_type == creditCard {
                        self.packageView.creditView.dataArray.append(model)
                        }else if model.card_type == debitCard {
                        self.packageView.debitView.dataArray.append(model)
                        }
                    }
                    
                    if self.packageView.creditView.dataArray.count != 0 {
                        self.packageView.creditView.isHidden = false
                    }
                    
                    self.packageView.creditView.reloadData()
                    self.packageView.debitView.reloadData()
                }
            }
            
        }) {
            
            KMTool.stop()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
        
    }

}

extension PackageViewController : PackageHeadViewDelegate {
    
    func selectBankCard(index: Int) {
        
        switch index {
        case 0:
            if self.packageView.creditView.dataArray.count == 0 {
                self.packageView.creditView.isHidden = true
            }else{
                self.packageView.creditView.isHidden = false
            }
            
            self.packageView.debitView.isHidden = true
        case 1:
            if self.packageView.debitView.dataArray.count == 0 {
                self.packageView.debitView.isHidden = true
            }else{
                self.packageView.debitView.isHidden = false
            }
            
            self.packageView.creditView.isHidden = true
        default:
            break
        }
    }
}

extension PackageViewController : CreditViewDelegate,DebitViewDelegate {
    
    //MARK: 删除储蓄卡
    func deleteDebitCard(index: Int) {
        
        KMTool.requestMethod(.post, URLString: url_deleteBankCard, parameters: ["id":self.packageView.debitView.dataArray[index].id ?? ""], success: { (value, json) in
            
            if json["status"].stringValue == "0" {
                self.getData()
            }
            
            KMTool.showText(_text: json["data"].stringValue)
            
        }) {
            
            KMTool.showText(_text: deleteFail)
        }
    }
    
    //MARK: 删除信用卡
    func deleteCreditCard(index: Int) {
        
        KMTool.requestMethod(.post, URLString: url_deleteBankCard, parameters: ["id":self.packageView.creditView.dataArray[index].id ?? ""], success: { (value, json) in
            
            if json["status"].stringValue == "0" {
                self.getData()
            }
            
            KMTool.showText(_text: json["data"].stringValue)
            
        }) {
            
            KMTool.showText(_text: deleteFail)
        }
    }
    
}
